package com.example.planetexploder.model.preference;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.planetexploder.bean.otherdata.Classification;

import java.util.ArrayList;
import java.util.Map;

import static com.example.planetexploder.constants.SPConst.CLASSIFICATIONS;
import static com.example.planetexploder.constants.SPConst.MAX_CLS_ID;

/**
 * 摘抄集分类的操作类
 * @author 蒋子涵
 * 2019.10.15
 */
public class Classifications {

    private static final String TAG = "asd";

    private Context mContext;

    public Classifications(Context mContext) {
        this.mContext = mContext;
    }

    /**
     * 储存分类信息
     * @param name      分类名
     */
    public void save_Classification(String name) {
        SharedPreferences sp = mContext.getSharedPreferences(CLASSIFICATIONS, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        //获取当前储存的书ID的最大值
        int max_num = sp.getInt(MAX_CLS_ID,2);

        //更新为最大值+1
        editor.putInt(MAX_CLS_ID,max_num+1);

        //存入分类信息键值对  编号ID：分类名
        editor.putString(max_num+"",name);
        editor.apply();
    }

    /**
     * 根据ID获取分类名称
     * @param ClassificationId  分类id
     * @return                   分类名
     */
    public String get_Classification(int ClassificationId) {
        SharedPreferences sp = mContext.getSharedPreferences(CLASSIFICATIONS, Context.MODE_PRIVATE);
        return sp.getString(ClassificationId+"","");
    }

    /**
     * 根据ID删除分类名称
     * @param classificationId  分类id
     */
    public void delete_Classification(int classificationId) {
        SharedPreferences sp = mContext.getSharedPreferences(CLASSIFICATIONS, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(classificationId + "");
        editor.apply();
    }

    /**
     * 修改分类名称
     * @param classificationId      分类id
     * @param name                   分类名
     */
    public void change_Classification(int classificationId, String name) {
        SharedPreferences sp = mContext.getSharedPreferences(CLASSIFICATIONS, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(classificationId+"",name);
        editor.apply();
    }

    /**
     * 获取当前ID最大值
     * @return                   id最大值
     */
    public int get_MaxID() {
        SharedPreferences sp = mContext.getSharedPreferences(CLASSIFICATIONS, Context.MODE_PRIVATE);
        return sp.getInt(MAX_CLS_ID,0);
    }

    /**
     * 获取所有分类
     * @return                   包含所有分类的数组
     */
    public ArrayList<Classification> get_allClassification() {
        SharedPreferences sp = mContext.getSharedPreferences(CLASSIFICATIONS, Context.MODE_PRIVATE);
        Map<String, ?> map = sp.getAll();
        ArrayList<Classification> result = new ArrayList<Classification>();

        for (Map.Entry entry:map.entrySet())
            if (!((String) entry.getKey()).equals(MAX_CLS_ID))
                result.add(new Classification(Integer.valueOf((String) entry.getKey()),(String) entry.getValue()));
        return result;
    }
}
